﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using BusinessLayer.Tables;
using System.Text;
using Data.Attributes;
using Data;

public partial class Customer : ChildPage
{
    #region Global Değişkenler
    StringBuilder sbHobi;
    StringBuilder sbGazete;
    StringBuilder sbTv;
    StringBuilder sbRadyo;
    #endregion

    #region Function
    private void KayitBul(string id)
    {
        Db_Musteri musteri = new Db_Musteri();
        musteri.MusteriKod = id;
        musteri.KayitBul();
        if (musteri.MusteriKod != null && musteri.MusteriAdi != null)
        {
            txtCustomerKod.Text = musteri.MusteriKod;
            txtev_tel.Text = musteri.EvTel;
            txtMusteriAdi.Text = musteri.MusteriAdi;
            txtis_tel.Text = musteri.IsTel;
            txtBagliMusteri.Text = musteri.BagliMusteri;
            txtcep_tel.Text = musteri.CepTel;
            drpMusteritip.SelectedItem.Value = musteri.MusteriTip;
            txtE_mail.Text = musteri.E_Mail;
            txtTcNo.Text = musteri.TcKimlikNo;
            txtWebAdres.Text = musteri.WebAdres;
            txtEvMahalle.Text = musteri.Ev_Mahalle;
            txtIsMahalle.Text = musteri.Is_Mahalle;
            txtEv_cadde.Text = musteri.Ev_Cadde;
            txtIsCadde.Text = musteri.Is_Cadde;
            txtEv_sokak.Text = musteri.Ev_Sokak;
            txtis_sokak.Text = musteri.Is_Sokak;
            txtev_ilce.Text = musteri.Ev_Ilce;
            txtis_ilce.Text = musteri.Is_Ilce;
            txtev_il.Text = musteri.Ev_Il;
            txtis_il.Text = musteri.Is_IL;
            txtdogum_yeri.Text = musteri.DogumYeri;
            txtvergi_numarasi.Text = musteri.VergiNumarasi;
            drpKartSubesi.SelectedValue = musteri.KartSube;
            drpAlisVeris.SelectedItem.Value = musteri.AlisverisTercihi;
            drpAlisVeris.SelectedItem.Text = musteri.NerdenBuldun;
            txtIsyeriAd.Text = musteri.IsyeriAd;
            drpogrenimdurumu.SelectedValue = musteri.OgrenimDurumu.ToString();
            if (musteri.Cinsiyeti == false)
                drpcinsiyet.SelectedIndex = 1;
            else
                drpcinsiyet.SelectedIndex = 0;
            if (musteri.MedeniHali == false)
                drpMedeniHali.SelectedIndex = 1;
            else
                drpMedeniHali.SelectedIndex = 0;
            txtTutulanTakim.Text = musteri.FutbolTakim;
            if (musteri.AylikGelir >= 0)
                drpaylikgelir.SelectedValue = musteri.AylikGelir.ToString();
            txtBilgi1.Text = musteri.Bilgi;
            txtBilgi2.Text = musteri.Bilgi2;
            if (musteri.Ev_PostaKodu != 0)
                txtEv_postakodu.Text = Convert.ToString(musteri.Ev_PostaKodu);
            if (musteri.Is_PostaKodu != 0)
                txtis_postakodu.Text = Convert.ToString(musteri.Is_PostaKodu);
            if (musteri.Ev_Site != 0)
                txtev_site.Text = Convert.ToString(musteri.Ev_Site);
            if (musteri.Ev_No != 0)
                txtev_no.Text = Convert.ToString(musteri.Ev_No);
            if (musteri.Is_Site != 0)
                txtis_site.Text = Convert.ToString(musteri.Is_Site);
            if (musteri.Is_No != 0)
                txtis_no.Text = Convert.ToString(musteri.Is_No);
            if (musteri.DogumTarihi != null)
                txtdogum_tarihi.Text = Convert.ToString(musteri.DogumTarihi);
            if (musteri.EvlilikTarihi != null)
                txtCalendarevliliktarihi.Text = Convert.ToString(musteri.EvlilikTarihi);
            if (musteri.Es_DogumGunu != null)
                txtEsDogumgunu.Text = Convert.ToString(musteri.Es_DogumGunu);
            if (musteri.KizCocukSayisi != 0)
                txtKizCocuk.Text = Convert.ToString(musteri.KizCocukSayisi);
            if (musteri.ErkekCocukSayisi != 0)
                txtErkekCocuk.Text = Convert.ToString(musteri.ErkekCocukSayisi);
            if (musteri.Radyo != null)
                ChekedYap(musteri.Radyo.Split(char.Parse(";")), txtDiger4);
            if (musteri.TvKanal != null)
                ChekedYap(musteri.TvKanal.Split(char.Parse(";")), txtDiger3);
            if (musteri.Gazete != null)
                ChekedYap(musteri.Gazete.Split(char.Parse(";")), txtDiger2);
            if (musteri.Hobiler != null)
                ChekedYap(musteri.Hobiler.Split(char.Parse(";")), txtdiger);

        }
    }
    private void KayitAdd()
    {
        Db_Musteri musteri = new Db_Musteri();
        sbHobi = new StringBuilder();
        sbGazete = new StringBuilder();
        sbTv = new StringBuilder();
        sbRadyo = new StringBuilder();
        if (txtCustomerKod.Text != "" && txtMusteriAdi.Text != "")
        {
            musteri.CreateUser = 1;
            musteri.MusteriKod = txtCustomerKod.Text;
            musteri.EvTel = txtev_tel.Text;
            musteri.MusteriAdi = txtMusteriAdi.Text;
            musteri.IsTel = txtis_tel.Text;
            musteri.BagliMusteri = txtBagliMusteri.Text;
            musteri.CepTel = txtcep_tel.Text;
            if (drpMusteritip.SelectedIndex != -1)
                musteri.MusteriTip = drpMusteritip.SelectedItem.Value;
            musteri.E_Mail = txtE_mail.Text;
            musteri.TcKimlikNo = txtTcNo.Text;
            musteri.WebAdres = txtWebAdres.Text;
            musteri.Ev_Mahalle = txtEvMahalle.Text;
            musteri.Is_Mahalle = txtIsMahalle.Text;
            musteri.Ev_Cadde = txtEv_cadde.Text;
            musteri.Is_Cadde = txtIsCadde.Text;
            musteri.Ev_Sokak = txtEv_sokak.Text;
            musteri.Is_Sokak = txtis_sokak.Text;
            if (txtEv_postakodu.Text != "")
                musteri.Ev_PostaKodu = Convert.ToInt32(txtEv_postakodu.Text);
            if (txtis_postakodu.Text != "")
                musteri.Is_PostaKodu = Convert.ToInt32(txtis_postakodu.Text);
            if (txtev_site.Text != "")
                musteri.Ev_Site = Convert.ToInt32(txtev_site.Text);
            if (txtis_site.Text != "")
                musteri.Is_Site = Convert.ToInt32(txtis_site.Text);
            if (txtev_no.Text != "")
                musteri.Ev_No = Convert.ToInt32(txtev_no.Text);
            if (txtis_no.Text != "")
                musteri.Is_No = Convert.ToInt32(txtis_no.Text);
            musteri.Ev_Ilce = txtev_ilce.Text;
            musteri.Is_Ilce = txtis_ilce.Text;
            musteri.Ev_Il = txtev_il.Text;
            musteri.Is_IL = txtis_il.Text;
            if (drpMedeniHali.SelectedIndex != -1)
                musteri.MedeniHali = bool.Parse(drpMedeniHali.SelectedValue);
            musteri.DogumYeri = txtdogum_yeri.Text;
            if (drpcinsiyet.SelectedIndex != -1)
                musteri.Cinsiyeti = bool.Parse(drpcinsiyet.SelectedValue);
            if (txtdogum_tarihi.Text != "")
                musteri.DogumTarihi = Convert.ToDateTime(txtdogum_tarihi.Text);
            else
                musteri.DogumTarihi = Convert.ToDateTime("01.01.1900");
            musteri.VergiDairesi = txtvergi_dairesi.Text;
            if (txtCalendarevliliktarihi.Text != "")
                musteri.EvlilikTarihi = Convert.ToDateTime(txtCalendarevliliktarihi.Text);
            else
                musteri.EvlilikTarihi = Convert.ToDateTime("01.01.1900");
            musteri.VergiNumarasi = txtvergi_numarasi.Text;
            if (txtEsDogumgunu.Text != "")
                musteri.Es_DogumGunu = Convert.ToDateTime(txtEsDogumgunu.Text);
            else
                musteri.Es_DogumGunu = Convert.ToDateTime("01.01.1900");
            if (drpogrenimdurumu.SelectedIndex != -1)
                musteri.OgrenimDurumu = Int16.Parse(drpogrenimdurumu.SelectedValue);
            if (txtKizCocuk.Text != "")
                musteri.KizCocukSayisi = Convert.ToInt32(txtKizCocuk.Text);
            if (drpaylikgelir.SelectedIndex != -1)
                musteri.AylikGelir = int.Parse(drpaylikgelir.SelectedItem.Value);
            if (txtErkekCocuk.Text != "")
                musteri.ErkekCocukSayisi = Convert.ToInt32(txtErkekCocuk.Text);
            if (drpMeslek.SelectedIndex != -1)
                musteri.Meslek = Convert.ToInt32(drpMeslek.SelectedValue);
            if (drpSektor.SelectedIndex != -1)
                musteri.Sektor = Convert.ToInt32(drpSektor.SelectedItem.Value);
            if (drpKartSubesi.SelectedIndex != -1)
                musteri.KartSube = drpKartSubesi.SelectedItem.Value;
            if (drpUyeOl.SelectedIndex != -1)
                musteri.HaberdarEt = bool.Parse(drpUyeOl.SelectedValue);
            if (drpAlisVeris.SelectedIndex != -1)
                musteri.AlisverisTercihi = drpAlisVeris.SelectedItem.Value;
            if (drpAlisVeris.SelectedIndex != -1)
                musteri.NerdenBuldun = drpAlisVeris.SelectedItem.Text;
            if (chksinema.Checked)
                sbHobi.Append(chksinema.Text + ";");
            if (chkSeyehat.Checked)
                sbHobi.Append(chkSeyehat.Text + ";");
            if (chkSpor.Checked)
                sbHobi.Append(chkSpor.Text + ";");
            if (chkKitap.Checked)
                sbHobi.Append(chkKitap.Text + ";");
            if (chktiyatro.Checked)
                sbHobi.Append(chktiyatro.Text + ";");
            if (chkOtomobil.Checked)
                sbHobi.Append(chkOtomobil.Text + ";");
            if (chkBilgisayar.Checked)
                sbHobi.Append(chkBilgisayar.Text + ";");
            if (chkMuzik.Checked)
                sbHobi.Append(chkMuzik.Text + ";");
            if (txtdiger.Text != "")
                sbHobi.Append(txtdiger.Text + ";");
            if (chkAksam.Checked)
                sbGazete.Append(chkAksam.Text + ";");
            if (chkHurriyet.Checked)
                sbGazete.Append(chkHurriyet.Text + ";");
            if (chkMilliyet.Checked)
                sbGazete.Append(chkMilliyet.Text + ";");
            if (chkPosta.Checked)
                sbGazete.Append(chkPosta.Text + ";");
            if (chkRadikal.Checked)
                sbGazete.Append(chkRadikal.Text + ";");
            if (chkSabah.Checked)
                sbGazete.Append(chkSabah.Text + ";");
            if (chkStar.Checked)
                sbGazete.Append(chkStar.Text + ";");
            if (chkTurkiye.Checked)
                sbGazete.Append(chkTurkiye.Text + ";");
            if (txtDiger2.Text != "")
                sbGazete.Append(txtDiger2.Text + ";");
            if (chkAtv.Checked)
                sbTv.Append(chkAtv.Text + ";");
            if (chkKanalD.Checked)
                sbTv.Append(chkKanalD.Text + ";");
            if (chkStar2.Checked)
                sbTv.Append(chkStar2.Text + ";");
            if (chkCNBC.Checked)
                sbTv.Append(chkCNBC.Text + ";");
            if (chkKanal7.Checked)
                sbTv.Append(chkKanal7.Text + ";");
            if (chkShow.Checked)
                sbTv.Append(chkShow.Text + ";");
            if (chkNtv.Checked)
                sbTv.Append(chkNtv.Text + ";");
            if (chkFox.Checked)
                sbTv.Append(chkFox.Text + ";");
            if (chkCnn.Checked)
                sbTv.Append(chkCnn.Text + ";");
            if (chkSamanyolu.Checked)
                sbTv.Append(chkSamanyolu.Text + ";");
            if (chkTrt.Checked)
                sbTv.Append(chkTrt.Text + ";");
            if (txtDiger3.Text != "")
                sbTv.Append(txtDiger3.Text);
            if (chkBest.Checked)
                sbRadyo.Append(chkBest.Text + ";");
            if (chkKral.Checked)
                sbRadyo.Append(chkKral.Text + ";");
            if (chkPiramit.Checked)
                sbRadyo.Append(chkPiramit.Text + ";");
            if (chkBurc.Checked)
                sbRadyo.Append(chkBurc.Text + ";");
            if (chkLalegul.Checked)
                sbRadyo.Append(chkLalegul.Text + ";");
            if (chkShowFm.Checked)
                sbRadyo.Append(chkShowFm.Text + ";");
            if (chkIstanbul.Checked)
                sbRadyo.Append(chkIstanbul.Text + ";");
            if (chkMarmara.Checked)
                sbRadyo.Append(chkSuperFm.Text + ";");
            if (chkJoy.Checked)
                sbRadyo.Append(chkJoy.Text + ";");
            if (chkMoral.Checked)
                sbRadyo.Append(chkMoral.Text + ";");
            if (chkTgrt.Checked)
                sbRadyo.Append(chkTgrt.Text + ";");
            if (txtDiger4.Text != "")
                sbRadyo.Append(txtDiger4.Text + ";");
            if (sbTv.Length > 0)
                sbTv.Remove(sbTv.Length - 1, 1);
            if (sbHobi.Length > 0)
                sbHobi.Remove(sbHobi.Length - 1, 1);
            if (sbGazete.Length > 0)
                sbGazete.Remove(sbGazete.Length - 1, 1);
            if (sbRadyo.Length > 0)
                sbRadyo.Remove(sbRadyo.Length - 1, 1);
            musteri.Radyo = sbRadyo.ToString();
            musteri.TvKanal = sbTv.ToString();
            musteri.Gazete = sbGazete.ToString();
            musteri.Hobiler = sbHobi.ToString();
            musteri.IsyeriAd = txtIsyeriAd.Text;
            musteri.FutbolTakim = txtTutulanTakim.Text;
            musteri.Bilgi = txtBilgi1.Text;
            musteri.Bilgi2 = txtBilgi2.Text;
            musteri.KartAdres = 1;
            musteri.Locked = true;
            musteri.UpdateDate = DateTime.Now;
            musteri.ID = 1;
            musteri.Hidden = true;
            musteri.CreateDate = DateTime.Now;
            musteri.Save();
            CreateArea();
        }
        else
            CrmControls.MessageBx.Show("Lütfen Müşteri Kodunu ve Müşteri Adını Boş Bırakmayın");
    }
    void DropDoldur()
    {
        Db_Depo depo = new Db_Depo();
        DataTable dt = depo.GetAllRecord_Datatable("depo_id ", "depo_adi ");
        for (int i = 0; i < dt.Rows.Count; i++)
        {
            ListItem lst = new ListItem();
            lst.Value = dt.Rows[i][0].ToString();
            lst.Text = dt.Rows[i][1].ToString();
            drpKartSubesi.Items.Add(lst);
        }

    }
    private void CreateArea()
    {
        for (int i = 0; i < MultiPage1.PageViews.Count; i++)
        {

            foreach (Control c in MultiPage1.PageViews[i].Controls)
            {
                if (c is CrmControls.CrmTextBox)
                {
                    CrmControls.CrmTextBox textbox = c as CrmControls.CrmTextBox;
                    textbox.Text = "";
                }
                if (c is CrmControls.CrmDropDown)
                {
                    CrmControls.CrmDropDown drop = c as CrmControls.CrmDropDown;
                    if (drop.SelectedIndex != -1)
                        drop.SelectedIndex = drop.Items.Count - 1; ;
                    switch (drop.ID)
                    {
                        case "drpcinsiyet":
                            if (drop.SelectedIndex != -1)
                                drop.SelectedIndex = 0;
                            break;
                        case "drpKartSubesi":
                            if (drop.SelectedIndex != -1)
                                drop.SelectedIndex = 0;
                            break;
                        case "drpAlisVeris":
                            if (drop.SelectedIndex != -1)
                                drop.SelectedIndex = 0;
                            break;
                        case "drpUyeOl":
                            if (drop.SelectedIndex != -1)
                                drop.SelectedIndex = 0;
                            break;
                    }
                }
                if (c is CrmControls.CrmCheckBox)
                {
                    CrmControls.CrmCheckBox check = c as CrmControls.CrmCheckBox;
                    check.Checked = false;
                }
            }
        }
    }
    #endregion

    protected void Page_Load(object sender, EventArgs e)
    {
        imgbtnKaydetKapat.Attributes.Add("onclick", "return Kontrol();");
        imgbtnKaydet.Attributes.Add("onclick", "return Kontrol();");
        imgbtnSil.Attributes.Add("onclick", "return SilKontrol();");
        DropDoldur();
        if (!IsPostBack)
        {
            if (Request.QueryString.Count > 0)
            {
                string id = Request.QueryString["id"];
                KayitBul(id);
            }
        }
    }

    public void ChekedYap(string[] s, TextBox diger)
    {
        for (int i = 0; i < s.Length; i++)
        {
            bool bulundu = false;
            foreach (System.Web.UI.Control cont in MultiPage1.Controls[2].Controls)
            {
                if (cont is CheckBox)
                {
                    CheckBox chk = cont as CheckBox;
                    if (chk.Text == s[i])
                    {
                        chk.Checked = true;
                        bulundu = true;
                        break;
                    }
                }
            }
            if (bulundu == false)
            {
                diger.Text = s[i];
            }
        }
    }

    protected void imgbtnKaydet_Click(object sender, ImageClickEventArgs e)
    {
        KayitAdd();
        this.ClientScript.RegisterStartupScript(typeof(Page), "Yenile", "Refresh();", true);
    }

    protected void imgbtnKaydetKapat_Click(object sender, ImageClickEventArgs e)
    {
        KayitAdd();
        this.ClientScript.RegisterStartupScript(typeof(Page), "Close", "PageClose();", true);
    }

    protected void imgbtnSil_Click(object sender, ImageClickEventArgs e)
    {
        Db_Musteri musteri = new Db_Musteri();
        musteri.MusteriKod = txtCustomerKod.Text;
        if (musteri.MusteriKod != "")
        {
            musteri.KayitSil();
            CreateArea();
        }

        else
            CrmControls.MessageBx.Show("Lütfen Müşteri Kodunu Girin");
        this.ClientScript.RegisterStartupScript(typeof(Page), "Tazele", "Refresh();", true);
    }

    protected void imgNewREcord_Click(object sender, ImageClickEventArgs e)
    {
        CreateArea();
    }
}
